Make xs_mkdir an xs_rm idempotent.
authorcl349@firebug.cl.cam.ac.uk <cl349@firebug.cl.cam.ac.uk>
Mon, 19 Sep 2005 14:25:29 +0000 (14:25 +0000)
committercl349@firebug.cl.cam.ac.uk <cl349@firebug.cl.cam.ac.uk>
Mon, 19 Sep 2005 14:25:29 +0000 (14:25 +0000)
When modifying libxenstore to transparently restart when the daemon dies,
it became apparent that life is simpler when all commands can simply be
restarted.  So this patch makes a slight semantic change to xs_rm and xs_mkdir:
xs_rm now succeeds if the file doesn't exist (as long as the parent exists),
and xs_mkdir succeeds if the directory already exists.
Noone should notice.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Signed-off-by: Christian Limpach <Christian.Limpach@cl.cam.ac.uk>
tools/xenstore/testsuite/02directory.test
tools/xenstore/testsuite/04rm.test
tools/xenstore/xenstored_core.c
tools/xenstore/xs.c
tools/xenstore/xs.h
tools/xenstore/xs_random.c

index 425026ffbefd7be4a5553ffd7465f01fe9452079..fd57bb8b78dfbe952681e37ad0f9c16785dcdead 100644 (file)
@@ -27,10 +27,8 @@ dir /dir
 expect contents2
 read /dir/test2
 
-# Creating dir over the top should fail.
-expect mkdir failed: File exists
+# Creating dir over the top should succeed.
 mkdir /dir
-expect mkdir failed: File exists
 mkdir /dir/test2
 
 # Mkdir implicitly creates directories.
index 186e9a9d756e55979053e7fd2c5381926ebb0cf0..bcc035bac290a76986e4edca01e228451bbfa827 100644 (file)
@@ -1,5 +1,4 @@
-# Remove non-existant fails.
-expect rm failed: No such file or directory
+# Remove non-existant is OK, as long as parent exists
 rm /test
 expect rm failed: No such file or directory
 rm /dir/test
index 564b7dce38f3b63c7b9f7637a2164f991cf0fadd..f5e7d0c9f3e6a81851cd94e2fd3ff685f69b51a2 100644 (file)
@@ -961,6 +961,13 @@ static char *tempdir(struct connection *conn,
        return dir;
 }
 
+static bool node_exists(struct connection *conn, const char *node)
+{
+       struct stat st;
+
+       return lstat(node_dir(conn->transaction, node), &st) == 0;
+}
+
 /* path, flags, data... */
 static void do_write(struct connection *conn, struct buffered_data *in)
 {
@@ -1050,7 +1057,6 @@ static void do_write(struct connection *conn, struct buffered_data *in)
 static void do_mkdir(struct connection *conn, const char *node)
 {
        char *dir;
-       struct stat st;
 
        node = canonicalize(conn, node);
        if (!check_node_perms(conn, node, XS_PERM_WRITE|XS_PERM_ENOENT_OK)) {
@@ -1066,9 +1072,9 @@ static void do_mkdir(struct connection *conn, const char *node)
        if (transaction_block(conn, node))
                return;
 
-       /* Must not already exist. */
-       if (lstat(node_dir(conn->transaction, node), &st) == 0) {
-               send_error(conn, EEXIST);
+       /* If it already exists, fine. */
+       if (node_exists(conn, node)) {
+               send_ack(conn, XS_MKDIR);
                return;
        }
 
@@ -1089,6 +1095,15 @@ static void do_rm(struct connection *conn, const char *node)
 
        node = canonicalize(conn, node);
        if (!check_node_perms(conn, node, XS_PERM_WRITE)) {
+               /* Didn't exist already?  Fine, if parent exists. */
+               if (errno == ENOENT) {
+                       if (node_exists(conn, get_parent(node))) {
+                               send_ack(conn, XS_RM);
+                               return;
+                       }
+                       /* Restore errno, just in case. */
+                       errno = ENOENT;
+               }
                send_error(conn, errno);
                return;
        }
index 9991cefc61b1c5c8fdaffac4c3c3c1e1907c20df..ad17e9d31af2ed1dc449be23375b89308906385c 100644 (file)
@@ -357,7 +357,7 @@ bool xs_write(struct xs_handle *h, const char *path,
 }
 
 /* Create a new directory.
- * Returns false on failure.
+ * Returns false on failure, or success if it already exists.
  */
 bool xs_mkdir(struct xs_handle *h, const char *path)
 {
@@ -365,7 +365,7 @@ bool xs_mkdir(struct xs_handle *h, const char *path)
 }
 
 /* Destroy a file or directory (directories must be empty).
- * Returns false on failure.
+ * Returns false on failure, or success if it doesn't exist.
  */
 bool xs_rm(struct xs_handle *h, const char *path)
 {
index 5999baaa00be5cf657375c25bd21aa7b0ef94526..98679eebd2f8faaacbdb0efaa1190164ac520116 100644 (file)
@@ -59,12 +59,12 @@ bool xs_write(struct xs_handle *h, const char *path, const void *data,
              unsigned int len, int createflags);
 
 /* Create a new directory.
- * Returns false on failure.
+ * Returns false on failure, or success if it already exists.
  */
 bool xs_mkdir(struct xs_handle *h, const char *path);
 
 /* Destroy a file or directory (and children).
- * Returns false on failure.
+ * Returns false on failure, or success if it doesn't exist.
  */
 bool xs_rm(struct xs_handle *h, const char *path);
 
index b8f39d36e73ad45cea8603e1c005de3351d68de9..7a2119ccf7c57a64ad140c34efa7a6116b2f8e60 100644 (file)
@@ -385,7 +385,7 @@ static bool file_mkdir(struct file_ops_info *info, const char *path)
 
        make_dirs(parent_filename(dirname));
        if (mkdir(dirname, 0700) != 0)
-               return false;
+               return (errno == EEXIST);
 
        init_perms(dirname);
        return true;
@@ -401,8 +401,11 @@ static bool file_rm(struct file_ops_info *info, const char *path)
                return false;
        }
 
-       if (lstat(filename, &st) != 0)
-               return false;
+       if (lstat(filename, &st) != 0) {
+               if (lstat(parent_filename(filename), &st) != 0)
+                       return false;
+               return true;
+       }
 
        if (!write_ok(info, path))
                return false;